package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.converter.DishConverter;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {
    private static final Logger log = LoggerFactory.getLogger(DishServiceImpl.class);
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = DishConverter.DishDtoconvertDish(dishDTO);
        dish.setStatus(StatusConstant.ENABLE);
        dishMapper.insert(dish);
        //获取insert语句的id主键值
        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null || flavors.size() > 0) {
            for (DishFlavor df : flavors) {
                df.setDishId(dishId);
            }
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page =dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 菜品批量删除
     * @param ids
     */
    @Override
    public void delectBatch(List<Long> ids) {
        dishIsDelectCheck(ids);
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorMapper.delectByDishId(id);
        }
    }


    /**
     * 菜品是否可以删除检查
     * @param ids
     */
    private void dishIsDelectCheck(List<Long> ids) {
        dishExitCheck(ids);
        SetmealIdByDishIdsCheck(ids);
    }

    /**
     * 检查菜品是否绑定套餐
     * @param ids
     */
    private void SetmealIdByDishIdsCheck(List<Long> ids) {
        List<Long> setmealIdByDishIds = setmealDishMapper.getSetmealIdByDishIds(ids);
        if(!Objects.isNull(setmealIdByDishIds)&&setmealIdByDishIds.size()>0){
            throw new RuntimeException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

    }

    /**
     * 检查菜品是否存在且是否处于在售状态
     * @param ids
     */
    private void dishExitCheck(List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(Objects.isNull(dish)){
                throw new RuntimeException("id"+id+"菜品不存在");
            } else if (dish.getStatus()==StatusConstant.ENABLE) {
                throw  new RuntimeException(MessageConstant.DISH_ON_SALE);
            }
        }
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdwithFlavor(Long id) {

        Dish dish = dishMapper.getById(id);

        List<DishFlavor> dishFlavors=dishFlavorMapper.getByDishId(id);

        DishVO dishVO=DishConverter.DishconvertDishVO(dish);
        dishVO.setFlavors(dishFlavors);
        return dishVO;

    }

    /**
     * 根据id修改基本信息和口味信息
     * @param dishDTO
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = DishConverter.DishDtoconvertDish(dishDTO);
        dish.setStatus(dishDTO.getStatus());
        dish.setId(dishDTO.getId());
        dishMapper.update(dish);
        dishFlavorMapper.delectByDishId(dishDTO.getId());
        List<DishFlavor> dishFlavors=dishDTO.getFlavors();
        if(dishFlavors!=null&&dishFlavors.size()>0){
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.insertBatch(dishFlavors);
        }
    }

    /**
     * 菜品起售和禁售
     * @param status
     * @param id
     */
    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder().id(id).status(status).build();
        //菜品起售或者禁用
        dishMapper.update(dish);
        //相关套餐也启用或者禁用
        Long setmealIdByDishId = setmealDishMapper.getSetmealIdByDishId(dish.getId());
        Setmeal setmeal = Setmeal.builder().id(setmealIdByDishId).status(status).build();
        setmealMapper.updateSetmeal(setmeal);

    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
